package com.deyinet.bdcms.dao.impl;


import com.deyinet.bdcms.core.dao.impl.BaseDaoImpl;
import com.deyinet.bdcms.dao.IDevicedataDao;
import com.deyinet.bdcms.entity.Devicedata;
import com.deyinet.bdcms.tools.CheckNull;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.*;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bacel on 2017/05/02.
 */
@Repository
public class DevicedataDao extends BaseDaoImpl<Devicedata> implements IDevicedataDao {

    @Override
    public List<Devicedata> findByExampleLike(Devicedata example, int pageNo, int pageSize, Timestamp beginTime, Timestamp endTime) {
        Criteria criteria = this.getSession().createCriteria(this.clazz);
        criteria.add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        criteria = setproper(example, criteria);
        if (CheckNull.check(beginTime)) {
            criteria.add(Restrictions.ge("createdate", beginTime));
        }
        if (CheckNull.check(endTime)) {
            criteria.add(Restrictions.le("createdate", new Timestamp(endTime.getTime() + 86400000)));
        }
        return createPageOrder(criteria, pageNo, pageSize, Order.desc("id")).list();
    }


    @Override
    public long countByExampleLike(Devicedata example, Timestamp beginTime, Timestamp endTime) {
        Criteria criteria = this.getSession().createCriteria(this.clazz);
        criteria.add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        criteria = setproper(example, criteria);
        if (CheckNull.check(beginTime)) {
            criteria.add(Restrictions.ge("createdate", beginTime));
        }
        if (CheckNull.check(endTime)) {
            criteria.add(Restrictions.le("createdate", new Timestamp(endTime.getTime() + 86400000)));
        }

        Long o = (Long) criteria.setProjection(Projections.count("id")).uniqueResult();
        return o.longValue();

    }

    @Override
    public List<Devicedata> getlinechartdata(String sn, Timestamp time1, Timestamp time2) {
        List<Devicedata> list = new ArrayList<>();
        String sql = "select t1.waterline,t1.createdate from devicedata t1 where t1.sn='" + sn + "'";
        if (time1 != null) {
            sql += " AND t1.createdate > '" + time1 + "'";
        }
        if (time2 != null) {
            sql += " AND t1.createdate < '" + time2 + "'";
        }
        Session session = getSession();
        List l = session.createSQLQuery(sql).list();
        for (int i = 0; i < l.size(); i++) {
            Object[] objects = (Object[]) l.get(i);
            Devicedata devicedata = new Devicedata();
            devicedata.setReport((Float) objects[0]);
            devicedata.setCreatedate((Timestamp) objects[1]);
            list.add(devicedata);
        }
        return list;
    }

    @Override
    public int countDeviceNormal(Timestamp beginTime, Timestamp endTime, int status) {
        String sql = "select COUNT(id) from devicedata where status = " + status;
        if (beginTime != null) {
            sql += " and createdate > '" + beginTime+"'" ;
        }
        if (endTime != null) {
            sql += " and createdate <'" + endTime + "'";
        }
        Session session = getSession();
        SQLQuery se = session.createSQLQuery(sql);
        List l = se.list();
        BigInteger objects = (BigInteger) l.get(0);
        return objects.intValue();
    }

    @Override
    public List<Devicedata> findByExampleLike(Devicedata example) {
        Criteria criteria = this.getSession().createCriteria(this.clazz);
        criteria.add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        criteria.createAlias("deviceByDevicesn", "device");
        criteria.add(Restrictions.like("device.devicename", "%" + example.getDeviceByDevicesn().getDevicename() + "%"));
        return criteria.list();
    }


    private static Criteria setproper(Devicedata example, Criteria criteria) {
        if (CheckNull.check(example.getDeviceByDevicesn())) {
            if (CheckNull.check(example.getDeviceByDevicesn().getDevicename())) {
                criteria.createAlias("deviceByDevicesn", "device");
                criteria.add(Restrictions.like("device.devicename", "%" + example.getDeviceByDevicesn().getDevicename() + "%"));
            }
            if (CheckNull.check(example.getDeviceByDevicesn().getDeviceownerByOwnerid())) {
                if (CheckNull.check(example.getDeviceByDevicesn().getDeviceownerByOwnerid().getName())) {
                    criteria.createAlias("deviceByDevicesn", "device");
                    criteria.createAlias("device.deviceownerByOwnerid", "owner");
                    criteria.add(Restrictions.like("owner.name", "%" + example.getDeviceByDevicesn().getDeviceownerByOwnerid().getName() + "%"));
                }
            }
            if (CheckNull.check(example.getDeviceByDevicesn().getDevicemodel())) {
                criteria.createAlias("deviceByDevicesn", "device");
                criteria.add(Restrictions.like("device.devicemodel", "%" + example.getDeviceByDevicesn().getDevicemodel() + "%"));
            }
        }
        return criteria;
    }


    @Override
    public List<Devicedata> findByExampleLikeImport(Devicedata example, Timestamp beginTime, Timestamp endTime) {
        Criteria criteria = this.getSession().createCriteria(this.clazz);
        criteria.add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        criteria = setproper(example, criteria);
        if (CheckNull.check(beginTime)) {
            criteria.add(Restrictions.ge("createdate", beginTime));
        }
        if (CheckNull.check(endTime)) {
            criteria.add(Restrictions.le("createdate", new Timestamp(endTime.getTime() + 86400000)));
        }
        return criteria.list();
    }
}